package com.wangpu.oa.web.servlet.usermanage;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wangpu.oa.dto.JSONResponse;
import com.wangpu.oa.dto.UserContext;
import com.wangpu.oa.entity.UserEntity;
import com.wangpu.oa.exception.OAException;
import com.wangpu.oa.service.Systemservice;
import com.wangpu.oa.service.impl.SystemServiceImpl;
import com.wangpu.oa.util.JSONUtil;

@WebServlet("/api/system/user/get")
public class CheckServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		JSONResponse result=new JSONResponse();
		try {
			String id=req.getParameter("id");
			
			Systemservice service=new SystemServiceImpl();
			HttpSession session=req.getSession(true);
			UserContext uc=(UserContext) session.getAttribute("CURRENT-USER");
			System.out.println(uc);
			UserEntity e=service.loadUser(id, uc.getId());
			
			Map<String, Object> dto=new HashMap<>(16,1);
			
			dto.put("id", e.getId());
			dto.put("account", e.getAccount());
			dto.put("name", e.getName());
			dto.put("avatar", e.getAvatar());
			dto.put("createTime", e.getCreateTime());
			
			dto.put("locked", e.isLocked());
			dto.put("lastLoginIp", e.getLastLoginIP());
			dto.put("lastLoginTime", e.getLastLoginTime());
			dto.put("lastModifyPasswordTime", e.getLastModifyPasswordTime());
			dto.put("phone", e.getPhone());
			
			dto.put("remark", e.getRemark());
			dto.put("securityEmail", e.getSecurityEmail());
			Map<String, Object> createUser=new HashMap<>(2,1);
			createUser.put("id", e.getCreateUser().getId());
			createUser.put("name", e.getCreateUser().getName());
			dto.put("createuser", createUser);
			dto.put("createUserId", e.getCreateUser().getId());
			
			result.setCode("ok");
			result.setData(dto);
			
		} catch (OAException e) {
			result.setCode("0001");
			result.setMessage(e.getMessage());
		} catch (Exception e) {
			result.setCode("error");
			result.setMessage("系统繁忙，请稍候再试!");
			//打印异常信息，用于调试
			e.printStackTrace();
		}finally {
			
			Writer out=resp.getWriter();
			out.write(JSONUtil.toJsonString(result));
			out.close();
		}
		
	}
}
